#include "NetworkForwardInterface.hpp"
#include <spdlog/spdlog.h>

NetworkForwardInterface::NetworkForwardInterface()
{
  connection=sdbus::createSessionBusConnection();
  proxy=sdbus::createProxy(*connection,InterfaceName,"/org/HBTY/NetworkForward");
}

NetworkForwardInterface::~NetworkForwardInterface()
{
  if(workThread.joinable())
  {
    connection->leaveEventLoop();
    workThread.join();
  }
}

long NetworkForwardInterface::GetNetworkPort()
{
  long res;
  proxy->callMethod("GetNetworkPort").onInterface(InterfaceName).storeResultsTo(res);
  return res;
}

long NetworkForwardInterface::CreateTask(std::string task_name)
{
  long res;
  proxy->callMethod("CreateTask").onInterface(InterfaceName).withArguments(task_name).storeResultsTo(res);
  return res;
}

std::string NetworkForwardInterface::GetStudentInfo(long stu_id)
{
  std::string res;
  proxy->callMethod("GetStudentInfo").onInterface(InterfaceName).withArguments(stu_id).storeResultsTo(res);
  return res;
}

void NetworkForwardInterface::MobileConnectChange(bool status)
{
  proxy->callMethod("MobileConnectChange").onInterface(InterfaceName).withArguments(status);
}

void NetworkForwardInterface::StartRecord(long task_id,long stu_id)
{
  proxy->callMethod("StartRecord").onInterface(InterfaceName).withArguments(task_id,stu_id);
}

void NetworkForwardInterface::StopRecord()
{
  proxy->callMethod("StopRecord").onInterface(InterfaceName);
}

bool NetworkForwardInterface::GetRecordStatus()
{
  bool res;
  proxy->callMethod("GetRecordStatus").onInterface(InterfaceName).storeResultsTo(res);
  return res;

}

bool NetworkForwardInterface::CheckStudentExists(long stuid)
{
  bool res;
  proxy->callMethod("CheckStudentExists").onInterface(InterfaceName).storeResultsTo(res);
  return res;

}

void NetworkForwardInterface::Boot()
{
  workThread=std::thread([this](){
    connection->enterEventLoop();
    spdlog::info("The sdbus connection exit");
  });
}
